<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\controller\shiyao;

use app\common\service\store\User as UserService;
use app\store\controller\Controller;
use app\store\model\shiyao\PayLog as PayLogModel;
use app\store\model\shiyao\ProjectTestee as TesteeModel;
use app\store\model\User as UserModel;
use think\facade\Db;

/**
 * 项目控制器
 * Class StoreUser
 * @package app\store\controller
 */
class Pay extends Controller {

	public function listAll() {
		$model = new PayLogModel;
		$list = $model->getListAll($this->request->param());
		return $this->renderSuccess(compact('list'));
	}

	/**
	 * 列表
	 * @return array
	 * @throws \think\db\exception\DbException
	 */
	public function list() {
		$model = new PayLogModel;
		$data = $model->getList($this->request->param());
		$list = $data['list'];
		$total_money = $data['total_money'];

		$store_user = UserService::getLoginInfo();
		$store_user = $store_user['user'];
		$user = Db::name('user')->where('mobile', $store_user['user_name'])->find();

		$params = $this->request->param();
		$can_do = 0;
		if (isset($params['center_id']) && $store_user['store_user_id'] != 10001) {
			//审批人员
			$userId = $user['user_id'];
			$centerIds = Db::name('shiyao_project_center_pm')
				->where('pm_id', $userId)
				->where('center_id', $params['center_id'])
				->find();

			$can_do = $centerIds ? 1 : 0;

		}
		$role = Db::name('store_user_role')->where('role_id', 'in', [10006, 10009])
			->where('store_user_id', $store_user['store_user_id'])->find();
		if ($role) {
			$can_do = 1;
		}

		return $this->renderSuccess(compact('list', 'can_do', 'total_money'));
	}
	public function state($companyId, array $logIds) {
		$model = new PayLogModel;
		if (!$model->setStatus($companyId, $logIds)) {
			return $this->renderError($model->getError() ?: '操作失败');
		}
		return $this->renderSuccess('操作成功');
	}
	public function audit(array $logIds, $state, $reason = '') {
		$model = new PayLogModel;
		if (!$model->setAudit($logIds, $state, $reason)) {
			return $this->renderError($model->getError() ?: '操作失败');
		}
		return $this->renderSuccess('操作成功');
	}

	public function auditPay() {
		$model = new PayLogModel;
		$data = $this->postForm();
		$reason = isset($data['pay_reason']) ? $data['pay_reason'] : '';

		if (!$model->setAuditPay($data['logIds'], $data['pay_status_pm'], $reason)) {
			return $this->renderError($model->getError() ?: '操作失败');
		}
		return $this->renderSuccess('操作成功');
	}
	public function rejectPay() {
		$model = new PayLogModel;
		$data = $this->postForm();
		$reason = isset($data['pay_reason']) ? $data['pay_reason'] : '';

		if (!$model->setRejectPay($data['logIds'], 2, $reason)) {
			return $this->renderError($model->getError() ?: '操作失败');
		}
		return $this->renderSuccess('操作成功');
	}
	/**
	 *
	 * @return array
	 */
	public function add() {
		// 新增记录
		$model = new PayLogModel;
		if ($model->add($this->postForm())) {
			return $this->renderSuccess('添加成功');
		}
		return $this->renderError($model->getError() ?: '添加失败');
	}

	public function getPayRoleInfo(int $id) {

		// 管理员详情
		$model = PayLogModel::detail($id);
		if ($model['payee_role'] == 0) {

			$user = TesteeModel::detail($model['testee_id'], ['idcardAvatar', 'idcardFile']);
			$info['user'] = $user;

		}

		if ($model['payee_role'] == 1) {
			//crc
			$info = Db::name('shiyao_project_center_crc')
				->where('user_id', $model['refer_id'])
				->where('center_id', $model['center_id'])
				->where('project_id', $model['project_id'])
				->field('name as bank_real_name,bank_num,bank_name,alipay,idcard')
				->find();

			$user = UserModel::detail($model['refer_id'], ['idcard1', 'idcard2']);
			$info['user'] = $user;

		}

		if ($model['payee_role'] == 2) {
			//pi
			$info = Db::name('shiyao_project_center_pi')

				->where('center_id', $model['center_id'])
				->where('project_id', $model['project_id'])
				->where('name', $model['payee'])
				->field('name as bank_real_name,bank_num,bank_name,alipay,idcard')
				->order('pi_id asc')
				->find();
			$user = UserModel::detail($info['refer_id'], ['idcard1', 'idcard2']);
			$info['user'] = $user;
		}

		if ($model['payee_role'] == 3) {
			//pm

			$info = Db::name('shiyao_project_center_pm')->alias('pm')
				->join('user', 'user.user_id=pm.pm_id')
				->where('pm.project_id', $model['project_id'])
				->where('pm.center_id', $model['center_id'])
				->where('user.nick_name', $model['payee'])
				->field('user.user_id')
				->find();

			$user = UserModel::detail($info['user_id'], ['idcard1', 'idcard2']);

			$info['user'] = $user;
		}

		return $this->renderSuccess(compact('info'));
	}

	/**
	 *
	 * @param int $projectId
	 * @return array
	 */
	public function edit(int $projectId) {
		// 管理员详情
		$model = ProjectModel::detail($projectId);
		// 更新记录
		if ($model->edit($this->postForm())) {
			return $this->renderSuccess('更新成功');
		}
		return $this->renderError($model->getError() ?: '更新失败');
	}

	public function delete(array $logIds) {
		$model = new PayLogModel;
		if (!$model->setDelete($logIds)) {
			return $this->renderError($model->getError() ?: '删除失败');
		}
		return $this->renderSuccess('删除成功');
	}

}
